package cn.iocoder.yudao.module.enterprise.controller.admin.service;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.service.vo.ServiceCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.service.vo.ServicePageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.service.vo.ServiceRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.service.vo.ServiceUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.service.ServiceConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.service.ServiceDO;
import cn.iocoder.yudao.module.enterprise.service.service.ServiceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name =  "管理后台 - 主营产品或服务")
@RestController
@RequestMapping("/enterprise/service")
@Validated
public class ServiceController {

    @Resource
    private ServiceService serviceService;

    @PostMapping("/create")
    @Operation(summary = "创建主营产品或服务")
    @PreAuthorize("@ss.hasPermission('enterprise:service:create')")
    public CommonResult<Long> createService(@Valid @RequestBody ServiceCreateReqVO createReqVO) {
        return success(serviceService.createService(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新主营产品或服务")
    @PreAuthorize("@ss.hasPermission('enterprise:service:update')")
    public CommonResult<Boolean> updateService(@Valid @RequestBody ServiceUpdateReqVO updateReqVO) {
        serviceService.updateService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除主营产品或服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('enterprise:service:delete')")
    public CommonResult<Boolean> deleteService(@RequestParam("id") Long id) {
        serviceService.deleteService(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得主营产品或服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:service:query')")
    public CommonResult<ServiceRespVO> getService(@RequestParam("id") Long id) {
        ServiceDO service = serviceService.getService(id);
        return success(ServiceConvert.INSTANCE.convert(service));
    }

    @GetMapping("/page")
    @Operation(summary = "获得主营产品或服务分页")
    @PreAuthorize("@ss.hasPermission('enterprise:service:query')")
    public CommonResult<PageResult<ServiceRespVO>> getServicePage(@Valid ServicePageReqVO pageVO) {
        PageResult<ServiceDO> pageResult = serviceService.getServicePage(pageVO);
        return success(ServiceConvert.INSTANCE.convertPage(pageResult));
    }

}
